package com.cmcc.es.app.service.impl;

import java.io.IOException;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import com.cmcc.es.app.mapper.PackageSyncMapper;
import com.cmcc.es.app.mapper.SupplierSyncMapper;
import com.cmcc.es.app.model.ProjectPackageInfo;
import com.cmcc.es.app.model.ProjectPackageSyncData;
import com.cmcc.es.app.model.SupplierInfo;
import com.cmcc.es.app.model.SupplierSyncData;
import com.cmcc.es.app.service.DataSyncService;

import cc.movein.common.utils.XmlTools;
/**
 * 包信息同步
 * @author XiaoPeng
 *
 */
@Service("packageSyncService")
public class packageSyncServiceImpl implements DataSyncService{
	private Log log = LogFactory.getLog(SupplierSyncServiceImpl.class);
	private static final String DATA_FORMAT = "yyyy-MM-dd HH:mm:ss";
	@Autowired
	private PackageSyncMapper packageSyncMapper;

	@Transactional
	@Override
	public void doSync(String xml) {
		String decodedXml = null;
		try {
			decodedXml = new String(Base64Utils.decodeFromString(xml), "utf-8");
			log.debug(decodedXml);
			ProjectPackageSyncData data = XmlTools.xmlStringToBean(decodedXml, ProjectPackageSyncData.class, DATA_FORMAT);
			log.info(data);
			List<ProjectPackageInfo> insertList = data.getInsertList();
			if (insertList != null && insertList.size() > 0) {
				packageSyncMapper.insertList(insertList);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
